package net.jgrm.product.controller;

import com.alibaba.fastjson.JSON;
import net.jgrm.idgenerator.service.IdGenerator;
import net.jgrm.idgenerator.service.impl.IdGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * ID生成器示例控制器
 * 用于展示如何使用分布式ID生成器
 */
@RestController
@RequestMapping("/api/id-generator")
public class IdGeneratorDemoController {

    // 方式1：直接使用IdGenerator接口
    @Autowired
    private IdGenerator idGenerator;

    // 方式2：使用更友好的IdGeneratorService
    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
     * 生成单个ID
     */
    @GetMapping("/generate")
    public Map<String, Object> generateId() {
        Long id = idGenerator.nextId();
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("id", id);
        result.put("message", "使用IdGenerator接口生成ID成功");
        return result;
    }

    /**
     * 生成指定业务类型的ID
     */
    @GetMapping("/generate/type")
    public Map<String, Object> generateBusinessId(@RequestParam(defaultValue = "default") String businessType) {
        Long id = idGenerator.nextId(businessType);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("businessType", businessType);
        result.put("id", id);
        result.put("message", "使用IdGenerator接口生成业务ID成功");
        return result;
    }

    /**
     * 使用Service生成单个ID
     */
    @GetMapping("/service/generate")
    public Map<String, Object> generateIdWithService() {
        Long id = idGeneratorService.generateId();
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("id", id);
        result.put("message", "使用IdGeneratorService生成ID成功");
        return result;
    }

    /**
     * 使用Service生成指定业务类型的ID
     */
    @GetMapping("/service/generate/type")
    public Map<String, Object> generateBusinessIdWithService(@RequestParam(defaultValue = "default") String businessType) {
        Long id = idGeneratorService.generateId(businessType);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("businessType", businessType);
        result.put("id", id);
        result.put("message", "使用IdGeneratorService生成业务ID成功");
        return result;
    }

    /**
     * 批量生成ID
     */
    @GetMapping("/service/batch/generate")
    public Map<String, Object> batchGenerateId(@RequestParam(defaultValue = "10") Integer count) {
        Long[] ids = idGeneratorService.batchGenerateId(count);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("count", count);
        result.put("ids", JSON.toJSONString(ids));

        // 将Long[]转换为String[]以避免JavaScript对大整数的处理问题
        String[] idsStr = new String[ids.length];
        for (int i = 0; i < ids.length; i++) {
            idsStr[i] = ids[i].toString();
        }
        result.put("ids2", idsStr);
        result.put("ids3", ids);
        result.put("message", "使用IdGeneratorService批量生成ID成功");
        return result;
    }

    /**
     * 批量生成指定业务类型的ID
     */
    @GetMapping("/service/batch/generate/type")
    public Map<String, Object> batchGenerateBusinessId(
            @RequestParam(defaultValue = "default") String businessType,
            @RequestParam(defaultValue = "10") Integer count) {
        Long[] ids = idGeneratorService.batchGenerateId(businessType, count);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("businessType", businessType);
        result.put("count", count);
        result.put("ids", ids);
        result.put("message", "使用IdGeneratorService批量生成业务ID成功");
        return result;
    }

    /**
     * 测试ID生成性能
     */
    @GetMapping("/performance")
    public Map<String, Object> testPerformance(@RequestParam(defaultValue = "10000") Integer count) {
        long start = System.currentTimeMillis();

        for (int i = 0; i < count; i++) {
            idGenerator.nextId();
        }

        long end = System.currentTimeMillis();
        double cost = (end - start) / 1000.0;
        double qps = count / cost;

        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("count", count);
        result.put("costSeconds", String.format("%.3f", cost));
        result.put("qps", String.format("%.2f", qps));
        result.put("message", "ID生成性能测试完成");
        return result;
    }
}